package com.qian.ddj.web.controller;

import com.qian.api.ddj.model.*;
import com.qian.api.ddj.service.ICashService;
import com.qian.api.ddj.service.IConfigService;
import com.qian.api.ddj.service.IOrderService;
import com.qian.api.ddj.service.IUserService;
import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.web.common.utils.HttpUtils;
import com.yaowk.web.system.security.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("ddj/customer/home")
@Api(tags = "客户首页")
public class CustomerHomeController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ICashService cashService;
    @Autowired
    private IConfigService configService;

    @GetMapping()
    @ApiOperation("获取首页数据")
    public Map index(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        User user = userService.findById(SecurityUtils.getUserId());
        result.put("balance", user.getBalance());

        BigDecimal successMoney = new BigDecimal(0); // 成功金额
        BigDecimal dealMoney = new BigDecimal(0); // 处理金额
        BigDecimal failMoney = new BigDecimal(0); // 失败金额
        BigDecimal lockMoney = new BigDecimal(0); // 冻结金额
        int dealNumber = 0; // 处理数量
        int successNumber = 0; // 成功数量

        // 构造搜索条件
        OrderQueryModel order = new OrderQueryModel();
        CashQueryModel cashQueryModel = new CashQueryModel();

        order.setUserId(user.getId());
        cashQueryModel.setUserId(user.getId());
        cashQueryModel.setStatus(StatusDict.NORMAL);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        cashQueryModel.setBeginTime(calendar.getTime());
        order.setBeginTime(calendar.getTime()); // 开始时间
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        order.setEndTime(calendar.getTime()); // 结束时间
        cashQueryModel.setEndTime(calendar.getTime());

        PageReq pageReq = new PageReq();
        pageReq.setPageSize(Integer.MAX_VALUE);
        pageReq.setPageNumber(1);
        PageResp<Order> page = orderService.findAll(order, pageReq);

        BigDecimal zero = new BigDecimal(0);
        BigDecimal hundred = new BigDecimal(100);

        // 统计数据订单数据
        for (Order o : page.getList()) {

            if (o.getStatus().equals(StatusDict.NORMAL) || o.getStatus().equals(StatusDict.WAIT_PAY)) {
                failMoney = failMoney.add(o.getAmount());
            } else {
                successNumber++;
                successMoney = successMoney.add(o.getAmount());
            }
        }

        // 获取提现数据
        PageResp<Cash> page2 = cashService.findAll(cashQueryModel, pageReq);
        for (Cash c : page2.getList()) {
            dealMoney = dealMoney.add(c.getTotal());
            dealNumber++;
        }

        // 构造返回值
        result.put("balance", user.getBalance());
        result.put("email", user.getEmail());
        result.put("successMoney", successMoney);
        result.put("dealMoney", dealMoney);
        result.put("failMoney", failMoney);
        result.put("lockMoney", orderService.countLockMoneyByUserId(user.getId()));
        result.put("dealNumber", dealNumber);
        result.put("successNumber", successNumber);
        result.put("cardNo", user.getCardNo());
        result.put("cardPerson", user.getCardPerson());
        result.put("cardPhone", user.getCardPhone());
        result.put("ip", HttpUtils.getRealIp(request));

        return result;
    }

    @GetMapping("channels")
    @ApiOperation("获取用户通道")
    public Set<CustomerChannel> channels() {
        Set<CustomerChannel> customerChannels = userService.findCustomerChannelById(SecurityUtils.getUserId());
        return customerChannels;
    }

    @GetMapping("/userInfo")
    @ApiOperation("获取用户信息")
    public Map getUserInfo(){
        User user = new User();
        user = userService.findById(SecurityUtils.getUserId());
        Config config = new Config();
        config.setName("publicKey");
        config = configService.findOne(config);
        Map<String,String> map = new LinkedHashMap<>();
        map.put("privateKey",user.getSecretKey());
        map.put("publicKey",user.getPublicKey());
        map.put("userId",user.getUsername());
        map.put("ptPublicKey",config.getValue());
        return map;
    }
}
